package com.huike.clues.service.impl;


import com.huike.clues.domain.TbAssignRecord;
import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.vo.ClueRecordedVo;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 线索跟进记录Service业务层处理
 * @date 2022-04-22
 */
@Service
public class TbClueTrackRecordServiceImpl implements ITbClueTrackRecordService {


    @Autowired
    private TbClueTrackRecordMapper tbClueTrackRecordMapper;

    @Autowired
    private TbClueMapper tbClueMapper;

    /**
     *  新增线索跟进记录
     * @param
     * @return
     */
    @Transactional
    @Override
    public void add(ClueRecordedVo clueRecordedVo) {

//修改  线索表 的基础信息
        TbClue tbClue = new TbClue();
/*        tbClue.setId(clueRecordedVo.getClueId());
        tbClue.setName(clueRecordedVo.getName());
        tbClue.setSex(clueRecordedVo.getSex());
        tbClue.setAge(clueRecordedVo.getAge());
        tbClue.setWeixin(clueRecordedVo.getWeixin());
        tbClue.setQq(clueRecordedVo.getQq());
        //设置下次跟进时间
        tbClue.setNextTime(clueRecordedVo.getNextTime());
        //意向等级和学科
        tbClue.setLevel(clueRecordedVo.getLevel());
        tbClue.setSubject(clueRecordedVo.getSubject());*/

        BeanUtils.copyProperties(clueRecordedVo,tbClue);

        //线索表 状态改为: 2 跟进中
        tbClue.setStatus("2");

        tbClueMapper.updatebyid(tbClue);

//添加线索跟进表记录
        //获取当前登录用户
        String username = SecurityUtils.getUsername();
        //设置操作者
        clueRecordedVo.setCreateBy(username);
        //线索跟进记录表---添加记录
        Date date = new Date();
        //设置创建时间
        clueRecordedVo.setCreateTime(date);
        clueRecordedVo.setType(TbAssignRecord.RecordType.CLUES.getValue());
        tbClueTrackRecordMapper.add(clueRecordedVo);
    }

    /**
     * 查询线索跟进记录列表
     * @param clueId
     * @return
     */
    @Override
    public List<ClueRecordedVo> list(Long clueId) {
        return tbClueTrackRecordMapper.list(clueId);
    }
}
